package kr.co.bettersoft.sendme.rest;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONObject;

import android.util.Log;
import kr.co.bettersoft.sendme.provider.SendMeCertification;
import kr.co.bettersoft.sendme.util.SendMeConstant;

/**
 * RESTful 메소드를 구현하는 클래스 이다.
 * 
 * @author johnkim
 *
 */
public class SendMeCertificationRest {

	public static String TAG = SendMeCertificationRest.class.getSimpleName();
	
	/**
	 * 인증번호를 요청 한다.
	 * 
	 * @param sendMeCertification
	 * @return
	 */
	public SendMeCertification requestCertification(SendMeCertification sendMeCertification) {
		Log.i(TAG, "requestCertification");
		
		String url = SendMeConstant.SERVER_URL
				+ SendMeConstant.MAPPING_CONTROLLER
				+ SendMeConstant.CERTIFICATION_CONTROLLER
				+ SendMeConstant.REQUEST_CERTIFICATION;
		
		SendMeCertification result = new SendMeCertification();
		
		try {
			JSONObject parameterJsonObject = new JSONObject();
			parameterJsonObject.put("phoneNumber", sendMeCertification.getPhoneNumber());
			parameterJsonObject.put("activateYn", sendMeCertification.getActivateYn());
			parameterJsonObject.put("modifyDate", sendMeCertification.getModifyDate());
			parameterJsonObject.put("registerDate", sendMeCertification.getRegisterDate());

			JSONObject resultJsonObject = new JSONObject(this.requestToServer(url, parameterJsonObject.toString()));
			result.setCertificationNumber(resultJsonObject.getString("certificationNumber"));
			result.setResult(resultJsonObject.getString("result"));
			Log.i(TAG, "result : " + result.getResult());
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	/**
	 * 승인을 요청 한다.
	 * 
	 * @param sendMeCertification
	 * @return
	 */
	public SendMeCertification requestAdmission(SendMeCertification sendMeCertification) {
		Log.i(TAG, "requestAdmission");
		
		String url = SendMeConstant.SERVER_URL
				+ SendMeConstant.MAPPING_CONTROLLER
				+ SendMeConstant.CERTIFICATION_CONTROLLER
				+ SendMeConstant.REQUEST_ADMISSION;
		
		SendMeCertification result = new SendMeCertification();
		
		try {
			JSONObject parameterJsonObject = new JSONObject();
			parameterJsonObject.put("phoneNumber", sendMeCertification.getPhoneNumber());
			parameterJsonObject.put("certificationNumber", sendMeCertification.getCertificationNumber());
			parameterJsonObject.put("activateYn", sendMeCertification.getActivateYn());
			
			JSONObject resultJsonObject = new JSONObject(this.requestToServer(url, parameterJsonObject.toString()));
			result.setResult(resultJsonObject.getString("result"));
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	/**
	 * http protocal 을 사용하는 서버 요청 공통 메소드.
	 * 요청 후 결과를 json string 으로 받아서 반환 한다.
	 * 
	 * @param url
	 * @param inputJsonString
	 * @return
	 */
	public String requestToServer(String url, String inputJsonString) {
		Log.i(TAG, "requestToServer");
		
		String result = "";
		
		try {
			URL postUrl = new URL(url);
			HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
	        connection.setDoOutput(true);
	        connection.setInstanceFollowRedirects(false);
	        connection.setRequestMethod("POST");
	        connection.setRequestProperty("Content-Type", "application/json");
	        OutputStream os = connection.getOutputStream();
	        os.write(inputJsonString.getBytes());
	        os.flush();
	        
	        if(connection != null) {
	        	connection.setConnectTimeout(3000);
	        	
	        	if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
	        		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	        		result = bufferedReader.readLine();
	        	}
	        }
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
}
